Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Class

Nested Classes

Nested Classes Nested classes, also known as inner classes, are classes defined within another class. They enable you to logically group classes that are only used in one place, increasing the use of encapsulation and creating more readable and maintainable code. Inner Classes An inner class is a non-static nested class. It can access all the members of the outer class, including private data members and methods. Here’s an example of an inner class:
Nested class structure class OuterClass { class InnerClass { // ... } }
Static Nested Classes Static nested classes are nested classes that are declared static. They are associated with their outer class and like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class. They can be accessed using the enclosing class name. Here’s an example of a static nested class:
Static nested class class OuterClass { static class StaticNestedClass { // ... } }
Local Classes Local classes are a special type of inner classes that are defined inside a block. This block is typically a method body, but it can also be a for loop or an if clause. The scope of a local class is restricted to the block they are defined in. Anonymous Classes An anonymous class is a class that is declared and instantiated simultaneously without providing a name. It serves the purpose of implementing an interface or extending a class while being used within a limited scope. Here’s an example of an anonymous class:
Anonymous class structure new InterfaceName() { // methods };

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops ★ types of class ★ nested class

Tutorials